package com.xinyue.game.gateway.server;

import com.xinyue.game.gateway.common.GameGatewayMessageRequestHeader;
import com.xinyue.game.gateway.common.GameGatewayMessageResponseHeader;
import com.xinyue.game.gateway.common.GatewayResponseUtil;
import com.xinyue.game.gateway.common.HandleMessageID;
import com.xinyue.game.message.GameServerMessageModel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class GatewayHeartbeatHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof GameServerMessageModel) {
            GameServerMessageModel<GameGatewayMessageRequestHeader> gatewayMessageWrapper = (GameServerMessageModel) msg;
            if (gatewayMessageWrapper.getHeader().getRequestUri().equals(HandleMessageID.HEART_BEAT_URI)) {
                // 收到心跳消息
                GameServerMessageModel<GameGatewayMessageResponseHeader> response = GatewayResponseUtil.getResponseMessageModel(gatewayMessageWrapper, 0, null);
                ctx.writeAndFlush(response);
            } else {
                ctx.fireChannelRead(msg);
            }
        } else {
            ctx.fireChannelRead(msg);
        }
    }
}
